<?php
/**
 * Created by PhpStorm.
 * User: wangcailin
 * Date: 2018/2/6
 * Time: 下午3:02
 */

namespace app\index\controller;


use app\common\controller\Frontend;

class Category extends Frontend
{
    protected $Article = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->Article = model('Article');
    }

    public function index($cid = null)
    {
        $category = null;
        $model = model('Category');
        switch ($cid){
            case '1':
                $category = 'automobile';
                break;
            case '6':
                $category = 'consumer';
                break;
            case '11':
                $category = 'finance';
                break;
            case '16':
                $category = 'indexenergy';
                break;
        }
        $this->view->assign('model', $model);
        return $this->view->fetch($category);
    }

    /**
     * 栏目文章列表
     * @param null $cid
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($cid = null)
    {
        $where = [
            'cid'       => $cid,
            'status'    => 'normal'
        ];
        $res = $this->Article->where($where)->order('weigh DESC,id DESC')->limit(1,10)->select();
        $top = $this->Article->where($where)->order('weigh DESC')->find();

        $this->assign('cid', $cid);
        $this->assign('top', $top);
        $this->assign('data', $res);
        return $this->view->fetch();
    }

    /**
     * 获取文章栏目列表
     * @return \think\response\Json
     */
    public function getListPage()
    {
        $page = input('page');
        $cid  = input('cid');
        $where = [
            'cid'       => $cid,
            'status'    => 'normal'
        ];
        $res = $this->Article->where($where)->order('weigh DESC,id DESC')->limit($page*10-1,10)->select();
        return json($res);
    }

}
